﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace Pong
{
    public class Ball : Sprite
    {
        private bool Survival { get; set; }
        public int SurvivalScore { get; set; }
        public Rectangle Rectangle { get; set; }
        public int WindowWidth { get; set; }
        public int WindowHeight { get; set; }
        public float Scale { get; set; }

        private int _startRandFunc = 1;
        private int _endRandFunc = 4;
        public Ball(int windowWidth, int windowHeight, float scale, bool survival)
        {
            this.WindowWidth = windowWidth;
            this.WindowHeight = windowHeight;
            this.Scale = scale;
            this.Survival = survival;
        }

        public override void Initialize()
        {
            base.Initialize();

            SurvivalScore = 0;

            Random rnd = new Random();
            int initialXDirection = 0;
            while (initialXDirection == 0)
                initialXDirection = rnd.Next(_startRandFunc, _endRandFunc) - 2;

            float initialYDirection = (float)rnd.NextDouble() * 2 - 1;

            this.Direction = new Vector2(initialXDirection, initialYDirection);
            this.Speed = WindowWidth * MyConstants.SPEED_MULTIPLIER;
        }

        public override void LoadContent(ContentManager content, string asset)
        {
            base.LoadContent(content, asset);
            float _positionX = WindowWidth / 2 - (this.Texture.Width * Scale) / 2;
            float _positionY = WindowHeight / 2 - (this.Texture.Height * Scale) / 2;

            this.Position = new Vector2(_positionX, _positionY);
            this.Rectangle = new Rectangle((int)this.Position.X,
                                            (int)this.Position.Y,
                                            (int)(this.Texture.Width * Scale),
                                            (int)(this.Texture.Height * Scale));
        }

        public void Update(GameTime gameTime, Rectangle rectangle1, Rectangle rectangle2,SoundEffect _ballWall)
        {
            float yRatio;

            // Ball collision with top and bottom of the screen
            if ((Position.Y <= 0 && Direction.Y < 0)
                || (Position.Y > WindowHeight - Texture.Height * Scale && Direction.Y > 0))
            {
                Direction = new Vector2(Direction.X, -Direction.Y);
            }

            // Ball collision detection with the bats
            if (Direction.X < 0 && rectangle1.Contains((int)Position.X, (int)Position.Y + (int)(Texture.Height * Scale / 2)))
            {
                if (Survival)
                    SurvivalScore++;

                yRatio = ((Position.Y + (Texture.Height * Scale) / 2) - rectangle1.Center.Y) / rectangle1.Height;

                Direction = new Vector2(1, yRatio);
                Speed += MyConstants.SPEED_INCREMENT;
                _ballWall.Play();
            }


            if (Direction.X > 0 && rectangle2.Contains((int)Position.X + (int)(Texture.Width * Scale), (int)Position.Y + (int)(Texture.Height * Scale / 2)))
            {
                yRatio = ((Position.Y + (Texture.Height * Scale) / 2) - rectangle2.Center.Y) / rectangle2.Height;

                Direction = new Vector2(-1, yRatio);
                Speed += MyConstants.SPEED_INCREMENT;
                _ballWall.Play();
            }

            base.Update(gameTime);
        }
    }
}
